<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="css/style.css"/>
    <link href="assets/css/codemirror.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/ace.min.css"/>
    <link rel="stylesheet" href="font/css/font-awesome.min.css"/>
    <!--[if lte IE 8]>
    <link rel="stylesheet" href="assets/css/ace-ie.min.css"/>
    <![endif]-->
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/typeahead-bs2.min.js"></script>
    <script src="assets/js/jquery.dataTables.min.js"></script>
    <script src="assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="assets/layer/layer.js" type="text/javascript"></script>
    <script src="js/H-ui.js" type="text/javascript"></script>
    <script src="js/displayPart.js" type="text/javascript"></script>
    <script src="assets/laydate/laydate.js" type="text/javascript"></script>
    <title>系统日志</title>
</head>
<body>
<div class="margin clearfix">
    <div id="system_style">
        <div class="search_style">

            <ul class="search_content clearfix">
                <li><label class="l_f">名称</label><input name="" type="text" class="text_add" style=" width:250px"></li>
                <li><label class="l_f">时间</label><input class="inline laydate-icon" id="start" style=" margin-left:10px;"></li>
                <li style="width:90px;">
                    <button type="button" class="btn_search" id="searchBtn"><i class="fa fa-search"></i>查询</button>
                </li>
            </ul>
        </div>
        <!--系统日志-->
        <div class="system_logs">
            <table class="table table-striped table-bordered table-hover" id="sample-table">
                <thead>
                <tr>
                    <th width="80px">ID</th>
                    <th width="120px">操作者</th>
                    <th width="120px">角色</th>
                    <th width="">操作内容</th>
                    <th width="150px">操作时间</th>
                </tr>
                </thead>
                <tbody id="sample-table-tbody">
                </tbody>
            </table>
        </div>
        <div id="pageBox" style="margin-top: 5%">
            <button id="upPage" onclick="up()" style="margin-left: 42%">上一页</button>
            <input type="text" id="page" style="width: 35px;text-align: center"/>&emsp;/<input type="text" id="total" style="width: 35px;text-align: center" disabled="disabled"/>
            <button id="nextPage" onclick="next()">下一页</button>
        </div>
    </div>
</div>
</body>
</html>
<script>
    laydate({
        elem: '#start',
        event: 'focus'
    });
    $(function () {
        var oTable1 = $('#sample-table').dataTable({
            "aaSorting": false,//默认第几个排序
            "bStateSave": true,//状态保存
            "aoColumnDefs": [
                //{"bVisible": false, "aTargets": [ 3 ]} //控制列的隐藏显示
                {"orderable": false, "aTargets": [0, 1, 2, 3]}// 制定列不参与排序
            ]
        });
    });

    var pageTotal;  //总页码
    var currentPage = 1;  //当前页码
    var upPage;     //上一页
    var nextPage;    //下一页
    $.ajax({
        url: "http://localhost:8080/SnacksShop/supManager.do?type=queryAllLogging",
        data: {
            currentPage:currentPage,
            pageSize:10
        },
        crossDomain: true,
        xhrFields: {
            withCredentials: true
        },
        type: "post",
        success: function (data) {
            var obj = JSON.parse(data);
            if (obj.managerLoggings.message == '200'){
                var object = obj.managerLoggings.object
                var datas = object.datas;
                info(datas);
                currentPage = object.currentPage;
                $("#page").val(currentPage);
                $("#total").val(obj.managerLoggings.object.totalPage);
                nextPage = object.nextPage;
                upPage = object.upPage;
                pageTotal = object.totalPage;
                btnDisabled(object);
            }
        },
        error: function (data) {
            layer.alert("访问服务器失败 \r\n",{
                title: '提示框',
                icon:0,
            });
        }
    });
    //显示
    function info(datas) {
        var tbody = document.getElementById("sample-table-tbody").childNodes;
        if (tbody != null){
            $("#sample-table-tbody").empty();
        }
            for (var i of datas){
            var type;
            var time = i.mangerLoggingTime.time;
            if (i.manager.manageType == '0'){
                type = '普通管理员';
            }else {
                type = '超级管理员';
            }
            var tr = document.createElement("tr");
            tr.innerHTML = "<td>" + i.mangerLoggingId + "</td>"+
                "<td>"+i.manager.managerName+"</td>"+
                "<td>"+type+"</td>"+
                "<td>"+i.mangerLoggingText+"</td>"+
                "<td>"+ new Date(time).toLocaleDateString()+"</td>";
            $("#sample-table-tbody").append(tr);
        }
    }
    var pageText = $("#page");
    //数字框的回车
    document.getElementById("page").addEventListener("keyup",function (event) {
        event.preventDefault();
        if (event.keyCode === 13){
            var values = pageText.val();
            if (isNaN(values)){
                layer.alert("不是数字 \r\n",{
                    title: '提示框',
                    icon:0,
                });
                $("#page").val(currentPage);
            }else {
                if (values > pageTotal){
                    $("#page").val(currentPage);
                    layer.alert("没有了 \r\n",{
                        title: '提示框',
                        icon:0,
                    });
                }else {
                    var currentPageTemp = $("#page").val();
                    var url = "http://localhost:8080/SnacksShop/supManager.do?type=queryAllLogging";
                    search(url,currentPageTemp);
                }
            }
        }
    });


    //判断按钮是否禁用
    function btnDisabled(object) {
        var currentPage = object.currentPage;
        var upPage = object.upPage;
        var nextPage = object.nextPage;
        var upElement = $("#upPage");
        var nextElement = $("#nextPage");
        if (currentPage == upPage){
            upElement.attr("disabled",true);
        }else {
            upElement.attr("disabled",false);
        }
        if (currentPage == nextPage){
            nextElement.attr("disabled",true);
        }else {
            nextElement.attr("disabled",false);
        }
    }
    var searchTextElement = $(".text_add");
    var searchDateElement = $("#start");


    document.getElementById("searchBtn").addEventListener("click",function (){
        var searchText = searchTextElement.val();
        var searchDate = searchDateElement.val();
        if (searchText != "" && searchDate == ""){   //查询名字
            searchByName(currentPage,searchText)
        }else if (searchText == "" && searchDate != ""){   //查询时间
            searchByDate(currentPage,searchDate)
        }else if (searchText != "" && searchDate != ""){    //查询名字和时间
           searchAll(currentPage,searchText,searchDate);
        }else {
            layer.alert("条件为空 \r\n",{
                title: '提示框',
                icon:0,
            });
        }
    });


    function search(url,page) {
        $.ajax({
            url: url,
            data:{
                currentPage:page,
                pageSize:10
            },
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            type: "post",
            success:function (data){
                var obj = JSON.parse(data);
                if (obj.managerLoggings.message == '200') {
                    var object = obj.managerLoggings.object;
                    $("#page").val(page);
                    $("#total").val(obj.managerLoggings.object.totalPage);
                    var datas = object.datas;
                    info(datas);
                    btnDisabled(object)
                }
            },
            error:function (data){

            }
        });
    }

    //上一页
    function up() {
        var searchText = $(".text_add").val();
        var searchDate = $("#start").val();
        var upPage = parseInt($("#page").val()) - 1;
        if (searchText != "" && searchDate == ""){
            searchByName(upPage,searchText);
        }else if (searchText == "" && searchDate != ""){
            searchByDate(upPage,searchDate);
        }else if (searchText != "" && searchDate != ""){
            searchAll(upPage,searchText,searchDate);
        }else if (searchText == "" && searchDate == ""){
            var url = "http://localhost:8080/SnacksShop/supManager.do?type=queryAllLogging";
            search(url,upPage);
        }

    }
    //下一页
    function next() {
        var searchText = $(".text_add").val();
        var searchDate = $("#start").val();
        var nextPage = parseInt($("#page").val()) + 1;
        if (searchText != "" && searchDate == ""){
            searchByName(nextPage,searchText);
        }else if (searchText == "" && searchDate != ""){
            searchByDate(nextPage,searchDate);
        }else if (searchText != "" && searchDate != ""){
            searchAll(nextPage,searchText,searchDate);
        }else if (searchText == "" && searchDate == ""){
            var url = "http://localhost:8080/SnacksShop/supManager.do?type=queryAllLogging";
            search(url,nextPage)
        }

    }

    //名字和时间一起查询
    function searchAll(currentPage,searchText,searchDate) {
        $.ajax({
            url: "http://localhost:8080/SnacksShop/supManager.do?type=queryByNameAndTimeLogging",
            data:{
                currentPage:currentPage,
                pageSize:10,
                searchText:searchText,
                searchDate:searchDate
            },
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            type: "post",
            success:function (data){
                var obj = JSON.parse(data);
                if (obj.managerLoggings.message == '200') {
                    var object = obj.managerLoggings.object;
                    $("#page").val(currentPage);
                    $("#total").val(obj.managerLoggings.object.totalPage);
                    var datas = object.datas;
                    info(datas);
                    btnDisabled(object);
                }else {
                    layer.alert("没有您想要的结果 \r\n",{
                        title: '提示框',
                        icon:0,
                    });
                }
            },
            error:function (data){

            }
        });
    }

    //时间查询
    function searchByDate(currentPage,searchDate) {
        console.log("传入的时间:"+currentPage);
        $.ajax({
            url: "http://localhost:8080/SnacksShop/supManager.do?type=queryByTimeLogging",
            data:{
                currentPage:currentPage,
                pageSize:10,
                searchDate:searchDate
            },
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            type: "post",
            success:function (data){
                var obj = JSON.parse(data);
                if (obj.managerLoggings.message == '200') {
                    var object = obj.managerLoggings.object;
                    $("#page").val(currentPage);
                    $("#total").val(obj.managerLoggings.object.totalPage);
                    var datas = object.datas;
                    info(datas);
                    btnDisabled(object);
                }else {
                    layer.alert("没有您想要的结果 \r\n",{
                        title: '提示框',
                        icon:0,
                    });
                }
            },
            error:function (data){

            }
        });
    }

    //名字查询
    function searchByName(currentPage,searchText) {
        $.ajax({
            url: "http://localhost:8080/SnacksShop/supManager.do?type=queryByNameLogging",
            data:{
                currentPage:currentPage,
                pageSize:10,
                searchText:searchText
            },
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            type: "post",
            success:function (data){
                var obj = JSON.parse(data);
                if (obj.managerLoggings.message == '200') {
                    var object = obj.managerLoggings.object
                    var datas = object.datas;
                    $("#page").val(currentPage);
                    $("#total").val(obj.managerLoggings.object.totalPage);
                    info(datas);
                    btnDisabled(object);
                }else {
                    layer.alert("没有您想要的结果 \r\n",{
                        title: '提示框',
                        icon:0,
                    });
                }
            },
            error:function (data){

            }
        });
    }

</script>
